<script setup lang="ts">
import { useAddressStore } from '@/stores/modules/address'
import type { addressItem } from '@/types/address'
import { pageUrl } from '@/utils/constants'

const emit = defineEmits<{
  (event: 'close'): void
}>()

//获取地址信息
const props = defineProps<{
  list?: addressItem[]
}>()

const addressStore = useAddressStore()

//选择收货地址
const onSelectAddress = (item: addressItem) => {
  addressStore.changeSelectedAddress(item)
  emit('close')
}

const toAddressList = () => {
  uni.navigateTo({
    url: `${pageUrl['address']}`,
  })

  emit('close')
}

useAddressStore().changeScene('order')
</script>

<template>
  <view class="address-panel">
    <text class="close icon-close" @tap="emit('close')"></text>
    <view class="title">配送至</view>
    <view class="content">
      <view v-if="props.list!.length > 0">
        <view class="item" v-for="item in props.list" :key="item.id" @tap="onSelectAddress(item)">
          <view class="user">{{ item.name }} {{ item.phone }}</view>
          <view class="address">{{ item.full_location }} {{ item.address }}</view>
          <text
            class="icon"
            :class="item.id === addressStore.selectedAddress?.id ? 'icon-checked' : 'icon-check'"
          ></text>
        </view>
      </view>
      <view v-else>
        <z-paging-empty-view
          empty-view-text="暂无收货地址"
          :empty-view-style="{
            top: '50rpx',
          }"
        />
      </view>
    </view>
  </view>

  <view class="footer">
    <view class="button primary">
      <view open-type="navigate" hover-class="navigator-hover" @tap="toAddressList">
        新增收货地址
      </view>
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 24rpx;
  top: 24rpx;
  font-size: 40rpx;
}

.content {
  min-height: 300rpx;
  max-height: 540rpx;
  overflow: auto;
  padding: 20rpx;

  .item {
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    background-image: url('~@/static/images/locate.png');
    position: relative;
  }
  .icon {
    color: #999;
    font-size: 40rpx;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 0;
  }
  .icon-checked {
    color: #ff5f3c;
  }
  .icon-ring {
    color: #444;
  }
  .user {
    font-size: 28rpx;
    color: #444;
    font-weight: 500;
  }
  .address {
    font-size: 26rpx;
    color: #666;
  }
  .empty-text {
    text-align: center;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #ff5f3c;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
